﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Web;
using System.Web.Mvc;

namespace PMS.Controllers.BD
{
    public class DefaultEnterRelationshipController : Controller
    {
        public ActionResult DefaultEnterRelationshipList()
        {
            return View();
        }
        //查询
        public ActionResult List(BD_DefaultEnterRelationship DefaultEnterRelationship)
        {
            List<SearchCondition> listSearch = new List<SearchCondition>();
            if (Request.Form["searchData"] != null)
            {
                string searchData = Request.Form["searchData"];
                listSearch = ModelTools.GetSearchContions(searchData);
            }
            listSearch.Add(new SearchCondition() { Field = "DelState", Operator = "=", Value = "0" });
            Expression<Func<BD_DefaultEnterRelationship, bool>> exp = SearchExpression.FindByGroup<BD_DefaultEnterRelationship>(DefaultEnterRelationship, listSearch);
            string OrderId = OperateContext.Sort == null ? "Id" : OperateContext.Sort;
            int count = 0;
            var list = BLLSession.Current.BD_DefaultEnterRelationshipBLL.GetPagedList(OperateContext.Page, OperateContext.Rows, exp, t => t.ID, true);
            count = BLLSession.Current.BD_DefaultEnterRelationshipBLL.GetCount(exp);
            return Json(new EasyuiDataPager() { rows = list, total = count });
        }
        //添加
        public ActionResult AddDefaultEnterRelationshipInfo(BD_DefaultEnterRelationship DefaultEnterRelationship)
        {
            DefaultEnterRelationship.EditTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
            DefaultEnterRelationship.EditPerName = OperateContext.sessUserInfo.Name;
            DefaultEnterRelationship.EditPerID = OperateContext.sessUserInfo.Id;
            DefaultEnterRelationship.DelState = "0";
            BLLSession.Current.BD_DefaultEnterRelationshipBLL.Add(DefaultEnterRelationship);
            return Json(new Message() { msg = "保存成功", state = "success" });
        }
        //修改
        public ActionResult EditDefaultEnterRelationshipInfo(BD_DefaultEnterRelationship DefaultEnterRelationship)
        {
            DefaultEnterRelationship.EditTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
            DefaultEnterRelationship.EditPerName = OperateContext.sessUserInfo.Name;
            DefaultEnterRelationship.EditPerID = OperateContext.sessUserInfo.Id;
            BLLSession.Current.BD_DefaultEnterRelationshipBLL.ModifyBy(DefaultEnterRelationship, t => t.ID == DefaultEnterRelationship.ID, "TotalIndex", "GradeTypeID", "ItemIndex", "TermTypeID", "IfMust", "EditPerID", "EditPerName", "EditTime", "Note", "DelState");
            return Json(new Message() { msg = "保存成功", state = "success" });
        }
        //删除
        public ActionResult DelDefaultEnterRelationshipInfo(int Id)
        {
            //BLLSession.Current.BD_DefaultEnterRelationshipBLL.DelBy(d => d.ID == Id);
            BD_DefaultEnterRelationship BD_DefaultEnterRelationship1 = DALSession.Current.BD_DefaultEnterRelationshipDAL.GetListBy(d => d.ID == Id).FirstOrDefault();
            BD_DefaultEnterRelationship1.EditTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
            BD_DefaultEnterRelationship1.EditPerName = OperateContext.sessUserInfo.Name;
            BD_DefaultEnterRelationship1.EditPerID = OperateContext.sessUserInfo.Id;
            BD_DefaultEnterRelationship1.DelState = "1";
            BLLSession.Current.BD_DefaultEnterRelationshipBLL.ModifyBy(BD_DefaultEnterRelationship1, t => t.ID == BD_DefaultEnterRelationship1.ID, "TotalIndex", "GradeTypeID", "ItemIndex", "TermTypeID", "IfMust", "EditPerID", "EditPerName", "EditTime", "Note", "DelState");
            return Json(new Message() { msg = "删除成功", state = "success" });
        }
        //获取所有年级
        public void GetAllGradeType()
        {
            Response.Write(JsonHelper.toJson(DALSession.Current.BD_GradeTypeDAL.GetListBy(t => t.DelState == "0").Select(t => new { value = t.ID, text = t.GradeType })));
        }
        //获取特定ID的年级
        public void GetGradeTypeName()
        {
            string GradeTypeID = Request.Form["GradeTypeID"];
            int GradeTypeIDint = Convert.ToInt32(GradeTypeID);
            var con = DALSession.Current.BD_GradeTypeDAL.GetListBy(c => c.ID == GradeTypeIDint);
            if (con.Count > 0)
            {
                Response.Write(JsonHelper.toJson(con.FirstOrDefault()));
            }
            else
            {
                Response.Write(JsonHelper.toJson(new { ID = GradeTypeID, GradeType = GradeTypeID }));
            }
        }

        //获取所有学期类型
        public void GetAllTermType()
        {
            Response.Write(JsonHelper.toJson(DALSession.Current.BD_TermTypeDAL.GetListBy(t => t.DelState == "0").Select(t => new { value = t.ID, text = t.TermType })));
        }
        //获取特定ID的学期类型
        public void GetTermTypeName()
        {
            string TermTypeID = Request.Form["TermTypeID"];
            int TermTypeIDint = Convert.ToInt32(TermTypeID);
            var con = DALSession.Current.BD_TermTypeDAL.GetListBy(c => c.ID == TermTypeIDint);
            if (con.Count > 0)
            {
                Response.Write(JsonHelper.toJson(con.FirstOrDefault()));
            }
            else
            {
                Response.Write(JsonHelper.toJson(new { ID = TermTypeID, TermType = TermTypeID }));
            }
        }
    }
}
